<?php
$xml=array();
include "./function.php";
$debug=0;
$debug2=0;
function ts($str){
global  $debug;
if($debug){
echo $str."\n";
 }else{


 }
}
function ts2($str){
global  $debug2;
if($debug2){
print_r( $str);
echo("\n");
 }else{


 }
}
function  findFile($dir){


	if(!is_dir($dir)){

		echo "sorry dir is  not Exist";
	}

	$dir = realpath($dir);

	ts2($dir);

	$dh=opendir($dir);

	if(!$dh){

		exit("sorry open dir fail\n");
	}

	while(($file = readdir($dh)) !== false){

		if($file == '.' || $file == '..'){

			continue;
		}

		$fullName=$dir."\\".$file;
		ts($fullName);

		if(is_dir($fullName)){

			ts("this is dir ".$fullName);
			findFile($fullName);
		}else if(is_file($fullName)){

			ts("this is file  name :".$fullName);
			handleFile($fullName);
		}

	}




}
function handleFile($fileName){
	$oldName=$fileName;
	ts2("fileName:".$fileName);
	$basePath=dirname($fileName);//this realpath have some problemc:/xx/xx/realpath  just for ./ ../
	ts2("basePath:".$basePath);
	if(!is_file($fileName)){

		exit("sorry this have some problem with file");
	}

	$shortName=basename($fileName);
	ts("short name :".$shortName);

	if(strpos($shortName, "smali") === false){

		ts($shortName." sorry this file is not we need change");
		return ;
	}

		$file =fopen($fileName,"r");
		fgets($file);
		fgets($file);
		$read=fgets($file);
		ts2("------------finally".$read);  
		$singleName=getKFName($read);
		if($singleName == -1){
			ts("sorry this not find  two yinhao");
			return ;
		}

		fclose($file);
        //handle $ file 

        if(($dollar=strpos($shortName,'$')) !== false){
            $smaliPos =strpos($shortName, ".smali");
        	$lastName=substr($shortName,$dollar,$smaliPos-$dollar);
        	ts2("$   -- name :".$lastName);
        	$singleName=$singleName.$lastName;
        	ts2("finally name :".$singleName);
        }
        ts2("$dollar:".$dollar);


        echo dirToPackage(dirname($fullName),"G:\anjava\project\wifi\smali");
		/*$newName=$basePath."\\".$singleName.".smali";
		ts2("newName:".$newName);
		ts2("basePath:".$basePath);
		if(rename($oldName, $newName)){

			ts2("**************************rename file success");
		}else{

			ts2("rename file failed");
		}*/

	

	//$file=fopen($fileName,"r");
	 // $read=fgets($file);
	  //ts($read);


}

function getKFName($str){
		$pos1=strpos($str,"\"");
		ts2($pos1);
		$pos2=strpos($str,".java",$pos1+1);
		ts2($pos2);

		if($pos1 === false || $pos2 === false){

			echo "sorry not fenhao taishao "."\n";

			return -1;
		}
		//$str='.source \'ConnectActivity.java"';
		$result=substr($str,$pos1+1,$pos2-$pos1-1);
		ts2($result);

		return $result;
	       //substr($str,)
}

findFile("G:\anjava\project\wifi\smali");








?>